layui.define(['jquery','table', 'form','common','layer','upload'], function(exports) {
    var $ = layui.$,
        larryms = layui.larryms,
        common = layui.common,
        form = layui.form;
        table = layui.table;
        layer=layui.layer;
        upload=layui.upload;

    //按钮组监听
    $('.larryms-btn-group .layui-btn').on('click', function() {
        var type = $(this).data('type'),
            url = $(this).data('url'),
            tit = $(this).children('cite').text();
        active[type] ? active[type].call(this, url, tit) : '';
    });
    var active = {
        add: function(url, tit) {
            var that = this;
            if ($(that).attr('data-flag') == 'exist') {
                var index = layui.cache.layerIndex,
                    $max = $max = $('#layui-layer' + index).find('.layui-layer-maxmin');
                $max.click();
                layui.cache.layerIndex = null;
            } else {
                $(that).attr('data-flag', 'exist');
                var index = larryms.open({
                    title: tit,
                    type: 2,
                    area: ['860px', '550px'],
                    content: url,
                    success: function(index, layero) {
                        form.render();
                    },
                    end: function() {
                        $(that).removeAttr("data-flag");
                        layui.cache.layerIndex = null;
                    }
                });
            }
        },
        del: function() {
            //批量删除
            var tableID = $(this).data('id'),
                checkStatus = table.checkStatus(tableID),
                data = checkStatus.data,
                newsId = [];
            var url = $(this).data('url');
            console.log(tableID);
            console.log(url);
            if (data.length > 0) {
                for (var i in data) {
                    //排除默认管理员
                    if(data[i].id !== 1){
                        newsId.push(data[i].id);
                    }
                }
                var idStr = newsId.join(',');
                if (newsId.length > 0) {
                    larryms.confirm('你确定要执行批量删除吗？', {
                        icon: 3,
                        title: '批量删除提示！'
                    }, function() {
                        $.ajax({
                            type: "POST",
                            url: url,
                            data: {ids:idStr},
                            async: true,//同步
                            dataType: "json",
                            contentType: "application/x-www-form-urlencoded; charset=utf-8",
                            cache: 'false',
                            timeout: 30000,
                            success: function (res) {
                                if (res.success) {
                                    layer.msg("删除成功",{icon:1,time:1500},function() {
                                        table.reload(tableID, {
                                            page: {
                                                curr: 1
                                            }
                                        });
                                    });
                                } else {
                                    layer.msg("删除失败", {icon: 5, time: 2000});
                                }
                            }
                        });
                    });
                }
            } else {
                larryms.msg('请至少选择一项，进行删除操作');
            }
        }
    };

    //搜索用户
    var pageTableID = $('.larryms-table-id').attr('id');
    $('#searchBtn').on('click', function() {
        var $searchVal = $('.searchVal').val();
        if ($searchVal !== '') {

            table.reload(pageTableID, {
                page: {
                    curr: 1
                },
                where: {
                    key: $searchVal
                }
            })
        } else {
            larryms.message('请输入要您要搜索的关键字');
        }
    });

    if (layui.cache.identified == 'api') {
        //  系统管理员
        var dataUrl = $('#'+pageTableID).data('url'),
            apiList = table.render({
                elem: '#'+pageTableID,
                id: pageTableID,
                cellMinWidth: 95,
                url: dataUrl,
                method: 'get',
                height: "full-200",
                page: true,
                limits: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
                limit: 10,
                cols: [
                    [{
                        type: "checkbox",
                        fixed: 'left',
                        width: 50
                    }, {
                        field: 'name',
                        title: '名称',
                        minWidth: 80,
                        align: 'center'
                    }, {
                        field: 'url',
                        title: 'URl',
                        align: 'center',
                        minWidth: 100
                    }, {
                        field: 'param',
                        title: '参数',
                        align: 'center',
                        minWidth: 60
                    }, {
                        field: 'result',
                        title: '返回',
                        align: 'center',
                        minWidth: 60
                    }
                    , {
                        field: 'status',
                        title: '状态',
                        width: 150,
                        align: 'center',
                        templet: '#statusBar',
                        event: 'modifyStatus'
                    }, {
                        title: '操作',
                        minWidth: 115,
                        templet: '#opBtnBar',
                        fixed: "right",
                        align: "center"
                        }
                    ]
                ]
            });
    }

    // 监听工具条
    table.on('tool('+pageTableID+')', function(obj) {
        var event = obj.event;
        var data = obj.data,
            tit='';
        if(layui.cache.identified == 'api'){
            tit = '编辑 <em class="tit">'+data.name+ '</em> 的接口信息';
        }
        switch (event) {
            case 'edit':
                var url =  $(this).data('url')+'?id='+data.id;
                editFun(tit,data,url);
                break;
            case 'detail':
                var url =  $(this).data('url')+'?id='+data.id+'&type=1';
                editFun(tit,data,url);
                break;
            case 'del':
                var url = $(this).data('url');
                console.log(url);
                delFun(tit,data,url,pageTableID);
            case 'modifyStatus':
                upStatusFun(tit,data);
                break;
        }
    });

    //跳转编辑页面
    function editFun(tit,data,url){
        var editIndex =  larryms.open({
            title: tit,
            type: 2,
            area: ['860px', '650px'],
            content: url,
            success: function(index, layero) {
                form.render();
            }
        });
    }
    //删除单条数据
    function delFun(tit,data,url,tblId){
        larryms.confirm('你确定要删除该条数据吗？',{icon: 3, title:'删除提示'},function(){
                var ids = {"id":data.id};
                $.post(url,ids,function(res){
                    var msg = res.msg == null ? "删除失败" : res.msg;
                    if (res.success) {
                        layer.msg("删除成功", {icon: 1, time: 1500}, function () {
                            table.reload(tblId, {});
                        })
                    } else {
                        layer.msg(msg, {icon: 5, time: 2000}, function () {
                            layer.closeAll();
                        });
                    }
                });
            });
    }
    //修改数据状态
    function upStatusFun(tit,data){
        layer.load(2);
        var status = parseInt();
        console.log(status);
        //    正常变成禁用 1 --> 0
        $.post('modifySatus.json', {
            id: data.id,
            status: 1-data.status
        }, function (res) {
            layer.closeAll('loading');
            if (!res.success) {
                layer.msg('修改状态失败' + res.msg, {icon: 5, time: 1500});
            }
        })
    }


    //切记：一定要输出模块,不然页面无法引用！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
    exports('module', {});
});